package fpt.edu.vn.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.Ratetopics;

/* 
 * AddRateAction.java
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class AddRateAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	// For SessionAware
	Map<String, Object> session;

	private int userId;
	private int rateValue;
	AddnewDao adddao = new AddnewDao();
	private int topicId;
	private InputStream inputStream;
	private Ratetopics rateTopic = new Ratetopics();
	private TopicsDao dao = new TopicsDao();

	private List<Object> rate;

	public String execute() {
		int userId_ = 0;
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userIdss")) {
			userId_ = (Integer) session.get("userIdss");
			setUserId(userId_);
		}

		if (adddao.checkRatetopics(userId, topicId)) {
			adddao.updateRate(userId, topicId, rateValue);

		} else {
			if (adddao.checkRatetopics(userId, topicId)) {
				adddao.updateRate(userId, topicId, rateValue);
			} else {
				rateTopic.setTopicId(topicId);
				rateTopic.setUsersId(userId);
				rateTopic.setPointRate(rateValue);
				adddao.addRate(rateTopic);
			}
		}
		this.rate = dao.getRate(topicId);
		String avg = "";
		String count = "";
		for (Iterator iterator1 = rate.iterator(); iterator1.hasNext();) {
			Object[] employee = (Object[]) iterator1.next();
			avg = employee[0].toString();
			count = employee[1].toString();
		}
		StringBuilder stbtemp = new StringBuilder();
		stbtemp.append(avg).append(",").append(count);
		inputStream = toUTF8InputStream(stbtemp.toString());
		return SUCCESS;

	}

	public static InputStream toUTF8InputStream(String str) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// UTF-8 should always be supported
			throw new AssertionError();
		}
		return is;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public int getRateValue() {
		return rateValue;
	}

	public void setRateValue(int rateValue) {
		this.rateValue = rateValue;
	}

	public List<Object> getRate() {
		return rate;
	}

	public void setRate(List<Object> rate) {
		this.rate = rate;
	}

}
